package com.sywb.chuangyebao.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.ShareUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ShareDialog extends BaseDialogFragment {

    public static final int TYPE_REPORT = 1;
    public static final int TYPE_DELETE = 2;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private String sharedType;// 分享类型：question=问答，video=视频，video_train=培训视频，audio_train培训音频，zt_train=专题，article=文章
    private int sharedId;
    private int showType;//0仅分享 1分享加删除 2分享加举报
    private String sharedTitle;
    private String sharedContent;
    private String sharedImage;

    public static ShareDialog newInstance(Object... pramars) {
        ShareDialog dialogFragment = new ShareDialog();
        dialogFragment.setArguments(getBundle(pramars));
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_share;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle != null) {
            sharedType = bundle.getString("p0");
            sharedId = bundle.getInt("p1", 0);
            showType = bundle.getInt("p2", 0);
            sharedTitle = bundle.getString("p3");
            sharedContent = bundle.getString("p4");
            sharedImage = bundle.getString("p5");
        } else {
            sharedType = getArguments().getString("p0");
            sharedId = getArguments().getInt("p1", 0);
            showType = getArguments().getInt("p2", 0);
            sharedTitle = getArguments().getString("p3");
            sharedContent = getArguments().getString("p4");
            sharedImage = getArguments().getString("p5");
        }

        ShareAdapter adapter = new ShareAdapter(mActivity);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 4));
        recyclerView.setAdapter(adapter);

        List<ShareItem> shareItems = new ArrayList<>();
        String[] plats = mActivity.getResources().getStringArray(R.array.share_plats);
        Integer[] platImages = new Integer[]{
                R.drawable.wechat,
                R.drawable.friendship,
                R.drawable.qq,
                R.drawable.sina,
                R.drawable.qq_space
        };

        for (int i = 0; i < 5; i++) {
            ShareItem shareItem = new ShareItem();
            shareItem.text = plats[i];
            shareItem.image = platImages[i];
            shareItems.add(shareItem);
        }

        switch (showType) {
            case TYPE_DELETE:
                ShareItem shareItem = new ShareItem();
                shareItem.text = "删除";
                shareItem.image = R.drawable.delete_fill;
                shareItems.add(shareItem);
                break;
            case TYPE_REPORT:
                ShareItem shareItem1 = new ShareItem();
                shareItem1.text = "举报";
                shareItem1.image = R.drawable.report;
                shareItems.add(shareItem1);
                break;
        }
        adapter.setDatas(shareItems);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                switch (i) {
                    case 0:
                        //微信好友
                        share(ShareUtils.SHARE_TYPE_WECHAT_FRIENDS);
                        break;
                    case 1:
                        //微信朋友圈
                        share(ShareUtils.SHARE_TYPE_WECHAT_FRIENDS_CIRCLE);
                        break;
                    case 2:
                        //QQ好友
                        share(ShareUtils.SHARE_TYPE_QQ_FRIENDS);
                        break;
                    case 3:
                        //新浪微博
                        share(ShareUtils.SHARE_TYPE_SINA);
                        break;
                    case 4:
                        //QQ空间
                        share(ShareUtils.SHARE_TYPE_QQ_ZONE);
                        break;
                    case 5:
                        if (onItemListener != null) {
                            onItemListener.onResult(ShareDialog.this);
                        }
                        if (Utils.checkIsNext(getMyFragmentManager())) {
                            if (showType == TYPE_DELETE && sharedType.equals(Constants.TOPIC_TYPE_QUESTION)) {
                                HttpUtils.deleteQuestion(sharedId, new HttpCallback<Object>() {

                                    @Override
                                    public void onSuccess(Object s) {
                                        ToastUtils.show(mActivity, "删除成功!");
                                        dismiss();
                                        RxBus.get().post(Constants.POST_DELETE_QUESTION, String.valueOf(sharedId));
                                    }

                                    @Override
                                    public void onError(String strMsg) {
                                        super.onError(strMsg);
                                        ToastUtils.show(mActivity, strMsg);
                                    }
                                });
                            } else {
                                HttpUtils.report(sharedType, sharedId, showType, new HttpCallback<Object>() {

                                    @Override
                                    public void onSuccess(Object o) {
                                        if (showType == TYPE_REPORT) {
                                            ToastUtils.show(mActivity, "举报成功！");
                                        } else {
                                            ToastUtils.show(mActivity, "删除成功!");
                                        }
                                        dismiss();
                                    }

                                    @Override
                                    public void onError(String strMsg) {
                                        super.onError(strMsg);
                                        ToastUtils.show(mActivity, strMsg);
                                    }
                                });
                            }
                        }
                        break;
                }
            }
        });
    }

    private void share(final int share2where) {
        HttpUtils.getShareUrl(sharedType, sharedId, new HttpCallback<String>() {

            @Override
            public void onSuccess(String sharedUrl) {
                if (TextUtils.isEmpty(sharedUrl)) {
                    ToastUtils.show(mActivity, "未获取到分享信息");
                    return;
                }
                switch (share2where) {
                    case ShareUtils.SHARE_TYPE_WECHAT_FRIENDS:
                        ShareUtils.shareToWechat_webpage(SendMessageToWX.Req.WXSceneSession, mActivity, sharedTitle, sharedContent, sharedImage, sharedUrl);
                        break;
                    case ShareUtils.SHARE_TYPE_WECHAT_FRIENDS_CIRCLE:
                        ShareUtils.shareToWechat_webpage(SendMessageToWX.Req.WXSceneTimeline, mActivity, sharedTitle, sharedContent, sharedImage, sharedUrl);
                        break;
                    case ShareUtils.SHARE_TYPE_QQ_FRIENDS:
                        ShareUtils.shareToQQ(mActivity, sharedTitle, sharedContent, sharedImage, sharedUrl);
                        break;
                    case ShareUtils.SHARE_TYPE_QQ_ZONE:
                        ShareUtils.shareToQQZone(mActivity, sharedTitle, sharedContent, sharedImage, sharedUrl);
                        break;
                    case ShareUtils.SHARE_TYPE_SINA:
                        ShareUtils.shareToSina(mActivity, sharedTitle, sharedContent, sharedImage, sharedUrl);
                        break;
                }
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                ToastUtils.show(mActivity, strMsg);
            }
        });
    }

    @OnClick({R.id.tv_cancel})
    @Override
    public void onClick(View v) {
        exit();
    }

    @Override
    public boolean isMatchParent() {
        return true;
    }

    class ShareItem {
        public Integer image;
        public String text;
    }

    class ShareAdapter extends BaseRecyclerAdapter<ShareItem> {

        ShareAdapter(Context context) {
            super(context, R.layout.item_share);
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int position, ShareItem model) {
            viewHolderHelper.setText(R.id.tv_share, model.text);
            viewHolderHelper.setImageResource(R.id.iv_share, model.image);
        }
    }
}
